package com.example.administrator.myapplication.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by LiJL on 2016/5/27.
 */
public class JsonUtils {
    private JSONArray getJsonArray(List<Object> listMap) {
        JSONArray array = new JSONArray();
        if(listMap != null) {
            Iterator var4 = listMap.iterator();

            while(var4.hasNext()) {
                Object obj = var4.next();
                if(obj instanceof Map) {
                    array.put(this.getJSONObject((Map)obj));
                } else {
                    array.put(obj);
                }
            }
        }

        return array;
    }

    private JSONObject getJSONObject(Map<String, Object> map) {
        if(map == null) {
            return new JSONObject();
        } else {
            JSONObject json = new JSONObject();

            try {
                Set e = map.keySet();
                Iterator iterator = e.iterator();

                while(iterator.hasNext()) {
                    String key = (String)iterator.next();
                    Object value = map.get(key);
                    if(value instanceof List) {
                        List list = (List)value;
                        json.put(key, this.getJsonArray(list));
                    } else if(value instanceof Map) {
                        json.put(key, this.getJSONObject((Map)value));
                    } else {
                        json.put(key, value);
                    }
                }
            } catch (Exception var8) {
                var8.printStackTrace();
            }

            return json;
        }
    }

    private static List<Object> jsonArray2List(JSONArray array) throws JSONException {
        ArrayList listMap = new ArrayList();
        int size = array.length();

        for(int i = 0; i < size; ++i) {
            Object object = array.get(i);
            if(object instanceof JSONObject) {
                JSONObject subObj = (JSONObject)object;
                listMap.add(json2Map(subObj));
            } else {
                listMap.add(object);
            }
        }

        return listMap;
    }

    public static Map<String, Object> json2Map(JSONObject jsonObject) throws JSONException {
        if(jsonObject == null) {
            return null;
        } else {
            HashMap map = new HashMap();
            Iterator it = jsonObject.keys();

            while(it.hasNext()) {
                String key = (String)it.next();
                Object obj = jsonObject.get(key);
                if(obj instanceof JSONObject) {
                    map.put(key, json2Map((JSONObject)obj));
                } else if(obj instanceof JSONArray) {
                    JSONArray array = (JSONArray)obj;
                    map.put(key, jsonArray2List(array));
                } else {
                    map.put(key, obj);
                }
            }

            return map;
        }
    }
}
